package com.rampage.multitimers.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

public class ClockView extends View {

	private Paint circlePaint;
	private Paint circleBgPaint;
	private Paint timePaint;
	private float percent = 0f;
	private int restingTime;
	private int alertTime;
	private boolean active = false;
	//private SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");

	public void setActive(boolean active) {
		this.active = active;
		invalidate();
	}

	public void update(int restingTime, int alertTime, float percent) {
		this.percent = 1f-percent;
		this.restingTime = restingTime;
		this.alertTime = alertTime;
		invalidate();
	}

	public ClockView(Context context) {
		super(context);
		init();
	}

	public ClockView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	private void init() {
		circlePaint = new Paint();
		circlePaint.setStyle(Style.STROKE);
		circlePaint.setStrokeWidth(4f);
		circlePaint.setColor(Color.WHITE);
		circlePaint.setAntiAlias(true);
		circlePaint.setShadowLayer(2f, 0, 0, Color.BLACK);

		circleBgPaint = new Paint();
		circleBgPaint.setStyle(Style.FILL_AND_STROKE);
		circleBgPaint.setColor(Color.rgb(0xDD, 0xDD, 0xDD));
		circleBgPaint.setAntiAlias(true);

		timePaint = new Paint();
		timePaint.setTextAlign(Align.CENTER);
		timePaint.setTextSize(30);
		timePaint.setTypeface(Typeface.DEFAULT_BOLD);
		timePaint.setColor(Color.WHITE);
		timePaint.setAntiAlias(true);
		timePaint.setShadowLayer(2f, 0, 0, Color.BLACK);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawColor(Color.WHITE);
		final float width = this.getMeasuredWidth()/2f;
		final float height = this.getMeasuredHeight()/2f;
		final float radius = Math.min(width, height)-10;
		final RectF oval = new RectF(width-radius, height-radius, width+radius, height+radius);
		if (restingTime > alertTime) {
			circleBgPaint.setColor(Color.rgb(0xDD, 0xDD, 0xDD));
		} else {
			circleBgPaint.setColor(Color.argb(0xBB, 0xFF, 0x00, 0x00));
		}
		canvas.drawArc(oval, -90, 360*percent, true, circleBgPaint);
		if (!active) {
			circlePaint.setColor(Color.WHITE);
		} else {
			if (restingTime > alertTime) {
				circlePaint.setColor(Color.rgb(0xDD, 0xDD, 0xDD));
			} else {
				circlePaint.setColor(Color.RED/*Color.rgb(0x00, 0x00, 0x00)*/);
			}
		}
		canvas.drawCircle(width, height, radius, circlePaint);
		canvas.drawText(calculateMinutes(), width, height+timePaint.descent()*1.5f, timePaint);
	}
	
	private String calculateMinutes() {
		if (restingTime < 60) {
			String str = "00:";
			final int seconds = restingTime;
			if (seconds < 10) {
				str += "0" + String.valueOf(seconds);
			} else {
				str += String.valueOf(seconds);
			}
			return str;
		} else {
			final int minutes = restingTime/60;
			final int seconds = restingTime%60;
			String str = "";
			if (minutes < 10) {
				str += "0" + String.valueOf(minutes);
			} else {
				str += String.valueOf(minutes);
			}
			str += ":";
			if (seconds < 10) {
				str += "0" + String.valueOf(seconds);
			} else {
				str += String.valueOf(seconds);
			}
			return str;
		}
	}
	
	@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureWidth(widthMeasureSpec),
                measureHeight(heightMeasureSpec));
    }
	
	/**
     * Determines the width of this view
     * @param measureSpec A measureSpec packed into an int
     * @return The width of the view, honoring constraints from measureSpec
     */
     private int measureWidth(int measureSpec) {
        int result = 150;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
            // Measure the text
            /*result = (int)timePaint.measureText("000:00") + getPaddingLeft()
                    + getPaddingRight();*/
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }

        return result;
    }

    /**
     * Determines the height of this view
     * @param measureSpec A measureSpec packed into an int
     * @return The height of the view, honoring constraints from measureSpec
     */
    private int measureHeight(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
            // Measure the text (beware: ascent is a negative number)
        	/*result = (int)timePaint.measureText("000:00") + getPaddingLeft()
            + getPaddingRight();*/
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

}
